﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using GPTAS.Core;
using GPTAS.Infrastructure;

namespace GPTASUI
{
    public partial class GPTAS : Form
    {
        public GPTAS()
        {
            InitializeComponent();
            WindowState = FormWindowState.Maximized;
        }

        private void GPTAS_Load(object sender, EventArgs e)
        {
            PopulateAllocationtree();
        }

        private void btnNewAllocation_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtNewAllocationName.Text))
            {
                Allocator.Instance.AddAllocation(txtNewAllocationName.Text);
                PopulateAllocationtree();
                NewAllocation frmNewAllocation = new NewAllocation();
                frmNewAllocation.Show();
            }
            else
            {
                MessageBox.Show("Please enter the name of the allocation");
            }
        }

        private void PopulateAllocationtree()
        {
            trvAllocations.Nodes.Clear();
            if (Allocator.Instance.Allocations.Count > 0)
            {
                foreach (var a in Allocator.Instance.Allocations)
                {
                    trvAllocations.Nodes.Add(a.Value.Name);
                }
            }
            
        }

        private void trvAllocations_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
           
        }

        private void trvAllocations_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeView node = (TreeView)sender;
            foreach (var a in Allocator.Instance.Allocations)
            {
                if (a.Value.Name == node.SelectedNode.Text)
                {
                    lblShowAllocationId.Text = a.Value.Id.ToString();
                    lblShowAllocationName.Text = a.Value.Name;
                    lblShowNoofGroups.Text = a.Value.Groups.Count.ToString();
                    lblShowNoofStudents.Text = a.Value.Items.Count.ToString();
                    lblShowStatus.Text = a.Value.Status;
                }
            }
        }
    }
}
